package org.eclipse.epsilon.emc.rdbms.core.persistanceobject;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.eclipse.epsilon.emc.rdbms.core.imodel.DBReflexiveModelElement;
import org.eclipse.epsilon.emc.rdbms.core.persistancebroker.PersistanceBroker;

public class PersistanceObject 
{		

	/** This method is used to pass the necessary information to PersistanceBroker to create the update statement.
	*@param element  - The specific element, row to be updated in the database.
	*@param properties  - the specific attributes and values to be updated.
	*/	 
	public void updateElement(DBReflexiveModelElement element, HashMap<String, Object> properties) throws SQLException
	{
		System.out.println(properties.get(0));
		PersistanceBroker dbtask= new PersistanceBroker();
		dbtask.updateObject(element,properties);
	}

	/** This method is used to pass the necessary information to PersistanceBroker to create the delete statement.
	*@param element  - The specific element, row to be deleted in the database.
	*@param properties  - the specific attributes and values to be updated.
	*/	 
	public String deleteElement(DBReflexiveModelElement element) throws SQLException
	{
		PersistanceBroker dbtask= new PersistanceBroker();
		dbtask.deleteObject(element);
		return null;
	}

	/** This method is used to pass the necessary information to PersistanceBroker to create the select statement.
	*@param element  - The specific element, row to be select in the database.
	*@param properties  - the specific attributes to be retrieved
	*@return results the results of the select statement
	*/	 
	public ResultSet getElement(DBReflexiveModelElement element,  ArrayList<String> properties) throws SQLException
	{
		PersistanceBroker dbtask= new PersistanceBroker();
		ResultSet results = dbtask.retrieveObject(element,properties);
		return results;
	}

	/** This method is used to pass the necessary information to PersistanceBroker to create the Insert statement.
	*@param element  - The specific element, row to be inserted into the database.
	*@param properties  - the specific attributes and values to be inserted which will override the default values from the typeMapper class.
	*@element the element that has been inserted
	*/	 
	public DBReflexiveModelElement addNewElement(DBReflexiveModelElement element,  HashMap<String, Object> properties) throws SQLException
	{
		PersistanceBroker dbtask= new PersistanceBroker();
		dbtask.saveObject(element,properties);
		return element;
	}

	/** This method is used to retrieve the primary Key information from a specific table
	*@param type - The specific type, table to get the primary key columns from 
	*@return hasType - The resultset of the primaryKey columns
	*/	 
	public ResultSet getKeys(String type)
	{
		PersistanceBroker dbtask= new PersistanceBroker();
		ResultSet hasType = dbtask.retrievehasType(type);	
		return hasType;
	}

	/** This method is used to pass the necessary information to PersistanceBroker to create the statement.
	*@param type - The specific type, table to be select in the database.
	*@param columns - the specific attributes to be retrieved
	*@param criteria - This is the specific attributes and values that will be specified in the where clause of the query. 
	*/	 
	public ResultSet getAllOfType(String type, ArrayList<String> columns, HashMap<String, Object> criteria) throws SQLException
	{	
		PersistanceBroker dbtask= new PersistanceBroker();
		ResultSet getAllOfType = dbtask.retrieveFromType(type,columns, criteria); //Returns a result set of all the data.
		return getAllOfType;
	}

	/** This method is used to pass the necessary information to PersistanceBroker to create the statement.
	*@param type - The specific type, table to be select in the database.
	*@param properties - the specific attributes to be updated in the database.
	*/	 	
	public void UpdateType(String type, HashMap<String, Object> properties) throws SQLException
	{	
		PersistanceBroker dbtask= new PersistanceBroker();
		dbtask.updateType(type,properties); //Returns a result set of all the data.
	}

	/** This method is used to pass the necessary information to PersistanceBroker to create the statement.
	*@param type - The specific type, table to be select in the database.
	*@return null
	*/	 
	public String deleteType(String type) throws SQLException
	{
		PersistanceBroker dbtask= new PersistanceBroker();
		dbtask.deleteType(type);
		return null;
	}

	/** This method is used to build the select statement in relation to the foreign key relationships.
	*@param element1 - This is the first element that we wish to join
	*@param element2 - This is the second element that we wish to join.
	*@return The result set of the select statement.
	*/	 
	public ResultSet getResultWithJoins(DBReflexiveModelElement element1,DBReflexiveModelElement element2) throws SQLException
	{
		PersistanceBroker dbtask= new PersistanceBroker();
		ResultSet getAllWithJoins = dbtask.retrieveFromTypeWithJoins(element1,element2.getType()); //Returns a result set of all the data.
		return getAllWithJoins;
	}

	public ResultSet getSQLColumns(DBReflexiveModelElement element) throws SQLException
	{
		PersistanceBroker dbtask= new PersistanceBroker();
		ResultSet SQLColumns = dbtask.connectTo().getSQLColumns(element.getType());
		//Returns a result set of all the data.
		return SQLColumns;
	}
	
	
	public ResultSet getFKColumns(DBReflexiveModelElement element) throws SQLException
	{
		PersistanceBroker dbtask= new PersistanceBroker();
		ResultSet FKCols = dbtask.connectTo().getExportedKeys(element);
		//Returns a result set of all the data.
		return FKCols;
	}
	

	public ResultSet getFKColumnsExported(DBReflexiveModelElement element) throws SQLException
	{
		PersistanceBroker dbtask= new PersistanceBroker();
		ResultSet FKCols = dbtask.connectTo().getExportedKeys(element);
		//Returns a result set of all the data.
		return FKCols;
	}

	public ResultSet getFKColumnsImported(DBReflexiveModelElement element) throws SQLException
	{
		PersistanceBroker dbtask= new PersistanceBroker();
		ResultSet FKCols = dbtask.connectTo().getImportedKeys(element.getType());
		//Returns a result set of all the data.
		return FKCols;
	}
	
	
}


